package com.gueei.applocker;

import gueei.binding.observables.BooleanObservable;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.gueei.applocker.AppLockerActivity;
import com.gueei.applocker.R;
public class LoginPageActivity extends Activity {
	private static final String DATABASE_TABLE = "loginTable";
	private static final String rname = null;
	ActivityStartingHandler ash;
	public static final String BlockedPackageName = "locked package name";
	public static final String BlockedActivityName = "locked activity name";
	public static final String ACTION_APPLICATION_PASSED = "com.gueei.applocker.applicationpassedtest";
	public static final String EXTRA_PACKAGE_NAME = "com.gueei.applocker.extra.package.name";
	public final BooleanObservable Passed = new BooleanObservable(false);
	DBAdapter db = new DBAdapter(this);
	
	SQLiteDatabase myDatabase;
	private EditText editText1, editText2;
	private void check(String un, String pw) {
		try {
			String value = "**Insert Correct Credentials**";
			db.open();
			String id = "0";
			String whereCheck = "u_name = '" + un + "' AND p_word = '" + pw	+ "'";
			Cursor cursor = db.getRow(DATABASE_TABLE, un, pw);
			String last=ash.getLastRunningPackage();
			 
			if (cursor != null && cursor.getCount() > 0) {
				
			/*	this.sendBroadcast(new Intent().setAction(ACTION_APPLICATION_PASSED).putExtra(EXTRA_PACKAGE_NAME, getIntent().getStringExtra(BlockedPackageName)));
				finish();				
				*/
				if (last.equals("com.gueei.applocker")) {
					Passed.set(true);
					Intent intent = new Intent(getApplicationContext(),AppLockerActivity.class);
					startActivity(intent);
				} else {Passed.set(true);
					getApplicationContext().sendBroadcast(new Intent().setAction(ACTION_APPLICATION_PASSED).putExtra(EXTRA_PACKAGE_NAME,getIntent().getStringExtra(BlockedPackageName)));
					finish();
				}
			} else {
				Toast.makeText(getBaseContext(), " INVALID CREDENTIALS",Toast.LENGTH_SHORT).show();
			}
			cursor.close();
		} catch (SQLException ex) {
			Toast.makeText(getBaseContext(), " INVALID CREDENTIALS",Toast.LENGTH_SHORT).show();
		}
		db.close();
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		ImageView image = (ImageView) findViewById(R.id.img);
		image.setImageResource(R.drawable.image47);
		final Button login = (Button) findViewById(R.id.button1);
		editText1 = (EditText) findViewById(R.id.editText1);
		editText2 = (EditText) findViewById(R.id.editText2);
		login.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String un = editText1.getText().toString();
				int user_length = un.length();
				switch (user_length) {
				case 0:	Toast.makeText(getBaseContext(), "Empty username",Toast.LENGTH_SHORT).show();
				break;
				default: {
					{
						String pn = editText2.getText().toString();
						int pass_length = pn.length();
						switch (pass_length) {
						case 0:	Toast.makeText(getBaseContext(),"  Enter Password", Toast.LENGTH_SHORT).show();
							break;
						default: {
							check(un, pn);
						}
						}
					}
					break;
				}
				}
			}
		});
	}
	public void onBackPressed() {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)
		.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		finish();
	}
}